package com.gec.system.controller;

import com.gec.model.pojo.TCategory;
import com.gec.system.Service.TCategoryService;
import com.gec.system.util.RandomIdUtil;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "前端分类控制器")
@RestController
@RequestMapping("/admin/system/tCategory")
public class TCategoryController {

    @Autowired
    private TCategoryService tCategoryService;

    @ApiOperation("加载列表")
    @GetMapping("/findAll")
    public Result findNodes() {
        List<TCategory> TCList = tCategoryService.findNodes();
        System.out.println("集合大小：" + TCList.size());
        return Result.ok(TCList);
    }

    @ApiOperation("根据id删除")
    @GetMapping("/removeById/{id}")
    public Result removeById(@PathVariable Long id) {
        boolean b = tCategoryService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("添加")
    @PostMapping("/addTCategory")
    public Result addTCategory(@RequestBody TCategory tCategory) {
        String id;
        // 随机id
        while (true) {
            id = RandomIdUtil.randomId();
            TCategory byId = tCategoryService.getById(id);
            if (byId == null) {
                break;
            }
        }
        System.out.println("分类id" + id);
        tCategory.setId(id);
        boolean b = tCategoryService.save(tCategory);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("修改")
    @PostMapping("/updateTCategory")
    public Result updateTCategory(@RequestBody TCategory tCategory) {
        boolean b = tCategoryService.updateById(tCategory);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    @ApiOperation("根据id获取分类信息")
    @GetMapping("/getTCategoryById/{id}")
    public Result getTCategoryById(@PathVariable String id) {
        TCategory tCategory = tCategoryService.getById(id);
        return Result.ok(tCategory);
    }
}
